<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\controller;

class IndexGoods extends \app\ApiRest
{
    protected $model;
    protected $cate_model;
    protected $car_model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
    }
    public function cateList()
    {
        $input = $this->_param;
        $dis = ["uniacid" => $this->_uniacid, "status" => 1, "cap_id" => $this->getCapInfo()["id"]];
        $data = $this->cate_model->where($dis)->order("id desc")->select()->toArray();
        return $this->success($data);
    }
    public function index()
    {
        $input = $this->_param;
        $lat = !empty($input["lat"]) ? $input["lat"] : 0;
        $lng = !empty($input["lng"]) ? $input["lng"] : 0;
        $dis = ["uniacid" => $this->_uniacid, "status" => 1, "cap_id" => $this->getCapInfo()["id"]];
        if(!empty($input["cate_id"])) {
            $dis["cate_id"] = $input["cate_id"];
        }
        $data["goods_list"] = $this->model->dataList($dis, 10);
        $data["store_info"] = $this->getCapInfo();
        $distance = getDistances($data["store_info"]["lng"], $data["store_info"]["lat"], $lng, $lat);
        $cap_model = new \app\shop\model\Cap();
        $data["store_info"]["distance"] = $cap_model->getDistanceAttr($distance);
        return $this->success($data);
    }
    public function goodsList()
    {
        $input = $this->_param;
        $dis = ["uniacid" => $this->_uniacid, "status" => 1, "cap_id" => $this->getCapInfo()["id"]];
        if(!empty($input["cate_id"])) {
            $dis["cate_id"] = $input["cate_id"];
        }
        $data = $this->model->dataList($dis, 10);
        return $this->success($data);
    }
    public function carInfo()
    {
        $car_info = $this->car_model->carPriceAndCount($this->getUserId(), $this->getCapInfo()["id"], 1);
        return $this->success($car_info);
    }
    public function goodsInfo()
    {
        $id = $this->request->param("id", "");
        if(empty($id)) {
            return $this->error("商品不存在");
        }
        $data = \app\massage\model\ShopGoods::getInfo(["id" => $id]);
        $data["images"] = json_decode($data["images"], true);
        return $this->success($data);
    }
    public function indexCapList()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 2];
        $dis[] = ["business_status", "=", 1];
        if(!empty($input["store_name"])) {
            $dis[] = ["store_name", "like", "%" . $input["store_name"] . "%"];
        }
        $lat = !empty($input["lat"]) ? $input["lat"] : 0;
        $lng = !empty($input["lng"]) ? $input["lng"] : 0;
        $alh = "(2 * 6378.137* ASIN(SQRT(POW(SIN(3.1415926535898*(" . $lat . "-lat)/360),2)+COS(3.1415926535898*" . $lat . "/180)* COS(" . $lat . " * 3.1415926535898/180)*POW(SIN(3.1415926535898*(" . $lng . "-lng)/360),2))))*1000 as distance";
        $alhs = "(2 * 6378.137* ASIN(SQRT(POW(SIN(3.1415926535898*(" . $lat . "-lat)/360),2)+COS(3.1415926535898*" . $lat . "/180)* COS(" . $lat . " * 3.1415926535898/180)*POW(SIN(3.1415926535898*(" . $lng . "-lng)/360),2))))*1000<20000";
        $cap_model = new \app\shop\model\Cap();
        $data = $cap_model->dataList($dis, $alh, $alhs, 10);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["distance"] = getDistances($v["lng"], $v["lat"], $lng, $lat);
                $v["distance"] = $cap_model->getDistanceAttr($v["distance"]);
            }
        }
        return $this->success($data);
    }
    public function selectCap()
    {
        $input = $this->_input;
        $user_model = new \app\shop\model\User();
        $res = $user_model->dataUpdate(["id" => $this->getUserId()], ["cap_id" => $input["cap_id"]]);
        return $this->success($res);
    }
    public function addCar()
    {
        $input = $this->_input;
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "cap_id" => $this->getCapInfo()["id"], "goods_id" => $input["goods_id"], "spe_id" => $input["spe_id"]];
        $info = $this->car_model->dataInfo($insert);
        if(!empty($info)) {
            $res = $this->car_model->dataUpdate(["id" => $info["id"]], ["goods_num" => $info["goods_num"] + $input["goods_num"]]);
        } else {
            $insert["goods_num"] = $input["goods_num"];
            $insert["status"] = 1;
            $res = $this->car_model->dataAdd($insert);
        }
        return $this->success($res);
    }
    public function delCar()
    {
        $input = $this->_input;
        $info = $this->car_model->dataInfo(["id" => $input["id"]]);
        if($input["goods_num"] < $info["goods_num"]) {
            $res = $this->car_model->dataUpdate(["id" => $info["id"]], ["goods_num" => $info["goods_num"] - $input["goods_num"]]);
        } else {
            $res = $this->car_model->where(["id" => $info["id"]])->delete();
        }
        return $this->success($res);
    }
    public function carUpdate()
    {
        $input = $this->_input;
        $res = $this->car_model->where("id", "in", $input["id"])->update(["status" => $input["status"]]);
        return $this->success($res);
    }
    public function delSomeCar()
    {
        $input = $this->_input;
        $dis = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "cap_id" => $this->getCapInfo()["id"]];
        $res = $this->car_model->where($dis)->delete();
        return $this->success($res);
    }
}

?>